<?php

namespace app\common\model\logic;

class MenuModel
{
    public function __construct()
    {
        $this->curModel = model('dao.menu');
    }

    public function getList($data)
    {
        $where[] = ['id', '>', 0];
        if (isset($data['status']) && !empty($data['status'])) {
            $where[] = ['status', '=', $data['status']];
        }
        $menuList = $this->curModel->where($where)->all();
        if (!$menuList->isEmpty()) {
            return $menuList;
        } else {
            exception(lang('GET_ERROR'), 0, 'app\\common\\exception\\DataEmptyException');
        }
    }

    public function getChildrenAll($parentId, $data)
    {
        $sum = $this->getChildrenSum($parentId, $data);
        if ($sum > 0) {
            $where = $this->getTreeWhere($parentId, $data);
            $menuList = $this->curModel->where($where)->order('sort desc,id asc')->all()->toArray();
            foreach ($menuList as &$ve) {
                $ve['class_type'] = 'menu';
                try {
                    $ve['api_id'] = $this->getPower(['id' => $ve['id']]);
                } catch (\Exception $e) {
                    $ve['api_id'] = 0;
                }

                $childSum = $this->getChildrenSum($ve['id'], $data);
                if ($childSum > 0) {
                    $ve['children'] = $this->getChildrenAll($ve['id'], $data);
                } else {
                    $ve['children'] = [];
                }
            }

            return $menuList;
        } else {
            return [];
        }
    }

    public function getChildrenSum($parentId, $data)
    {
        $where = $this->getTreeWhere($parentId, $data);

        return $this->curModel->where($where)->count();
    }

    private function getTreeWhere($parentId, $data)
    {
        $where[] = ['parent_id', '=', $parentId];
        if (isset($data['type']) && !empty($data['type'])) {
            $where[] = ['type', '=', $data['type']];
        }
        if (isset($data['status']) && !empty($data['status'])) {
            $where[] = ['status', '=', $data['status']];
        }

        return $where;
    }

    public function saveAdd($data)
    {
        $data['status'] = 1;

        return $this->curModel->allowField(true)->isUpdate(false)->save($data);
    }

    public function getRow($data)
    {
        $where[] = ['id', '=', $data['id']];

        $result = $this->getLine($where);
        if ($result !== false) {
            return $result;
        } else {
            exception(lang('GET_ERROR'), 0, 'app\\common\\exception\\DataEmptyException');
        }
    }

    public function getLine($where, $fields = '*')
    {
        return $this->curModel->where($where)->field($fields)->find();
    }

    //保存编辑
    public function saveEdit($data)
    {
        $this->getRow($data);

        $result = $this->curModel->allowField(true)->isUpdate(true)->save($data);
        if ($result !== false) {
            return $result;
        } else {
            exception(lang('SAVE_ERROR'), 0, 'app\\common\\exception\\DataEmptyException');
        }
    }

    //保存删除
    public function saveDel($data)
    {
        $this->getRow($data);
        if ($this->getChildrenSum($data['id'], '') > 0) {
            exception('存在子页面或者操作,不能删除', 0, 'app\\common\\exception\\LogicException');
        }
        $result = $this->curModel->destroy($data['id']);
        if ($result !== false) {
            return $result;
        } else {
            exception(lang('DEL_ERROR'), 0, 'app\\common\\exception\\LogicException');
        }
    }

    //获取权限
    public function getPower($data)
    {
        $bind = model('server.auth_apis_bind');

        return  $bind->getMenuPower($data);
    }

    //保存权限
    public function savePower($data)
    {
        $bind = model('server.auth_apis_bind');

        return $bind->saveMenuPower($data);
    }
}
